
//package com.zoweunion.mechanic.socket;
//import java.io.IOException;
//import java.io.InputStream;
//import java.net.ServerSocket;
//import java.net.Socket;
//import java.time.LocalDateTime;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.boot.CommandLineRunner;
//import org.springframework.core.annotation.Order;
//import org.springframework.stereotype.Component;
//@Order(1)
//@Component
//@SuppressWarnings({"resource"})
////接收硬件那边传输过来的数据解析并处理
//public class SocketServer implements CommandLineRunner {
//
//	protected static Logger logger = LoggerFactory.getLogger(SocketServer.class);
//
//	public static Map<String, String> map = new HashMap<String, String>();
//
//	public static List<Map<String, String>> mapList = new ArrayList<Map<String,String>>();
//
//	@Override
//	public void run(String... args) throws Exception {
//		int port = 8088;
//		ServerSocket server = null;
//		try {
//			server = new ServerSocket(port);
//		} catch (IOException e1) {
//			e1.printStackTrace();
//		}
//		while(true) {
//			try {
//				System.out.println("服务端坐等郑凯那边学习机数据的到来!!!!!!");
//				System.out.println("----------------"+LocalDateTime.now()+"------------------");
//				java.net.Socket socket = server.accept();
//				new Thread(new Task(socket)).start();
//			} catch (IOException e) {
//
//				e.printStackTrace();
//			}
//		}
//
//	}
//
//	static class Task implements Runnable {
//		private Socket socket;
//		public Task(Socket socket) {
//			this.socket = socket;
//		}
//		@Override
//		public void run() {
//			try {
//				handlerSocket();
//			}
//			catch (Exception e) {
//				e.printStackTrace();
//			}
//		}
//
//		private void handlerSocket() throws Exception {
//			InputStream in = socket.getInputStream();
//			byte[] buffer = new byte[in.available()];
//			int len = -1;
//			int index;
//			boolean flag = true;
//			int mark = 2;
//			Loop: while(true) {
//				while ((len = in.read(buffer)) != -1) {
//					if (mark % 2 != 0) {
//						flag = (flag == true ? false : true);
//					}
//					//输出获取到所有字节  16进制
//					System.out.println(new String(buffer));
//					if ((index = new String(buffer).indexOf("close")) != -1) {
//						break Loop;
//					}
//					if ((index = new String(buffer).indexOf("#")) != -1) {
//
//						break;
//					}
//				}
//				if(flag) {
//					System.out.println("Form Cliect[port:" + socket.getPort()+ "] 消息内容:" + buffer);
//					logger.info(""+buffer);
//					logger.info(new String(buffer));
//					System.out.println("----------------"+LocalDateTime.now()+"------------------");
//					byte[] destIdBuffer = new byte[2];
//					byte[] destDataBuffer = new byte[8];
//					for (int i = 0; i < buffer.length/12; i++) {
//						System.arraycopy(buffer, 4+12*i, destIdBuffer, 0, 2);
//						System.arraycopy(buffer, 7+12*i, destDataBuffer, 0, 8);
//						String key = new String(destIdBuffer,2);
//						String value = new String(destDataBuffer,8);
//						//String key = bytesToHexString(destIdBuffer);
//						//String value = bytesToHexString(destDataBuffer);
//						map.put(key, value);
//						System.out.println("Form Cliect[port:" + socket.getPort()+ "] 消息内容:" +key);
//						System.out.println("----------------"+LocalDateTime.now()+"------------------");
//						System.out.println("Form Cliect[port:" + socket.getPort()+ "] 消息内容:" + value);
//					}
//
//					mapList.add(map);
//					flag = (flag == true ? false : true);
//					mark++;
//					System.out.println("Form Cliect[port:" + socket.getPort()+ "] 消息内容:" + map);
//				}
//
//			}
//			// 回应一下客户端
////			Writer writer = new OutputStreamWriter(socket.getOutputStream(),
////					"UTF-8");
////			writer.write(String.format("Hi,zhengkai,nihao", socket.getPort()));
////			writer.flush();
////			writer.close();
//			System.out.println("To Cliect[port:" + socket.getPort() + "] 回复客户端的消息发送成功");
//			System.out.println("---------------------------------"+Thread.currentThread()+"-------------------------------");
//			socket.close();
//		}
//	}
//
//	public static final String bytesToHexString(byte[] bArray) {
//		StringBuffer sb = new StringBuffer(bArray.length);
//		String sTemp;
//		for (int i = 0; i < bArray.length; i++) {
//			sTemp =Integer.toHexString(0xFF & bArray[i]);
//			if (sTemp.length() < 2)
//				sb.append(0);
//			sb.append(sTemp.toUpperCase());
//		}
//		return sb.toString();
//	}
//
//	public static final List<Map<String, String>> getListInfo(){
//		return mapList;
//	}
//
//}=======
/*package com.zoweunion.mechanic.socket;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Order(1)
@Component
@SuppressWarnings({"resource"})
//接收硬件那边传输过来的数据解析并处理
public class SocketServer implements CommandLineRunner {

	protected static Logger logger = LoggerFactory.getLogger(SocketServer.class);

	public static Map<String, String> map = new HashMap<String, String>();

	public static List<Map<String, String>> mapList = new ArrayList<Map<String,String>>();

	@Override
	public void run(String... args) throws Exception {
		int port = 8088;
		ServerSocket server = null;
		try {
			server = new ServerSocket(port);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		while(true) {
			try {
				System.out.println("服务端坐等郑凯那边学习机数据的到来!!!!!!");
				System.out.println("----------------"+LocalDateTime.now()+"------------------");
				java.net.Socket socket = server.accept();
				new Thread(new Task(socket)).start();	
			} catch (IOException e) {

				e.printStackTrace();
			}
		}

	}

	static class Task implements Runnable {
		private Socket socket;
		public Task(Socket socket) {
			this.socket = socket;
		}
		@Override
		public void run() {
			try {
				handlerSocket();
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}

		private void handlerSocket() throws Exception {
			InputStream in = socket.getInputStream();
			byte[] buffer = new byte[in.available()];
			int len = -1;		
			int index;
			boolean flag = true;
			int mark = 2;
			Loop: while(true) {
				while ((len = in.read(buffer)) != -1) {
					if (mark % 2 != 0) {
						flag = (flag == true ? false : true);
					}
					//输出获取到所有字节  16进制
					System.out.println(new String(buffer));
					if ((index = new String(buffer).indexOf("close")) != -1) {
						break Loop;
					}
					if ((index = new String(buffer).indexOf("#")) != -1) {

						break;
					}					
				}
				if(flag) {
					System.out.println("Form Cliect[port:" + socket.getPort()+ "] 消息内容:" + buffer);
					logger.info(""+buffer);
					logger.info(new String(buffer)); 
					System.out.println("----------------"+LocalDateTime.now()+"------------------");
					byte[] destIdBuffer = new byte[2];
					byte[] destDataBuffer = new byte[8];
					for (int i = 0; i < buffer.length/12; i++) {
						System.arraycopy(buffer, 4+12*i, destIdBuffer, 0, 2);
						System.arraycopy(buffer, 7+12*i, destDataBuffer, 0, 8);
						String key = new String(destIdBuffer,2);
						String value = new String(destDataBuffer,8);
						//String key = bytesToHexString(destIdBuffer);
						//String value = bytesToHexString(destDataBuffer);
						map.put(key, value);
						System.out.println("Form Cliect[port:" + socket.getPort()+ "] 消息内容:" +key);
						System.out.println("----------------"+LocalDateTime.now()+"------------------");
						System.out.println("Form Cliect[port:" + socket.getPort()+ "] 消息内容:" + value);
					}

					mapList.add(map);
					flag = (flag == true ? false : true);
					mark++;
					System.out.println("Form Cliect[port:" + socket.getPort()+ "] 消息内容:" + map);
				}

			}
			// 回应一下客户端
//			Writer writer = new OutputStreamWriter(socket.getOutputStream(),
//					"UTF-8");
//			writer.write(String.format("Hi,zhengkai,nihao", socket.getPort()));
//			writer.flush();		
//			writer.close();
			System.out.println("To Cliect[port:" + socket.getPort() + "] 回复客户端的消息发送成功");
			System.out.println("---------------------------------"+Thread.currentThread()+"-------------------------------");		
			socket.close();
		}
	}

	public static final String bytesToHexString(byte[] bArray) {
		StringBuffer sb = new StringBuffer(bArray.length);
		String sTemp;
		for (int i = 0; i < bArray.length; i++) {
			sTemp =Integer.toHexString(0xFF & bArray[i]);
			if (sTemp.length() < 2)
				sb.append(0);
			sb.append(sTemp.toUpperCase());
		}
		return sb.toString();
	}

	public static final List<Map<String, String>> getListInfo(){
		return mapList;
	}

}*/
